package com.xiguthings.waka.web.controller.option;

import javax.annotation.Resource;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.Paper;
import com.xiguthings.waka.po.PaperExample;
import com.xiguthings.waka.po.QuestionOption;
import com.xiguthings.waka.po.QuestionOptionExample;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.web.controller.paper.PaperVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.service.option.IQuestionOptionService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/option")
@Api(tags = "答题选项")
public class QuestionOptionController extends AbstractController {
    @Resource
    private IQuestionOptionService questionOptionService;

    /**
     * 创建查询条件
     *
     * @param questionOptionVo
     * @param example
     * @throws NumberFormatException
     */
    private void queryConditions(QuestionOptionVo questionOptionVo, QuestionOptionExample example) throws NumberFormatException {

        QuestionOptionExample.Criteria criteria = example.createCriteria();
        // 如果查询条件为空的
        if (questionOptionVo == null) {
            // 则查询所有未被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
            return;
        }

        // 如果有查询条件
        QuestionOption questionOption = questionOptionVo.getQuestionOption();
        // 添加查询条件
        if (questionOption != null) {
            if (StringUtils.isNotBlank(questionOption.getOptionId())) {
                criteria.andOptionIdEqualTo(questionOption.getOptionId());
            }
            if (StringUtils.isNotBlank(questionOption.getQuestionId())) {
                criteria.andQuestionIdEqualTo(questionOption.getQuestionId());
            }
            if (StringUtils.isNotBlank(questionOption.getContent())) {
                criteria.andContentLike("%" + questionOption.getContent() + "%");
            }
            if (StringUtils.isNotBlank(questionOption.getAnalysis())) {
                criteria.andAnalysisLike("%" + questionOption.getAnalysis() + "%");
            }
            if (questionOption.getScore() != null) {
                criteria.andScoreEqualTo(questionOption.getScore());
            }
            if (questionOption.getRank() != null) {
                criteria.andRankEqualTo(questionOption.getRank());
            }

        }

        if (StringUtils.isNotBlank(questionOptionVo.getInStatus())) {
            // 添加包含的状态
            String statuses = questionOptionVo.getInStatus();
            String[] statusArr = statuses.split(",");
            List<Integer> statusList = new ArrayList<>();
            for (String str : statusArr) {
                statusList.add(new Integer(str));
            }
            criteria.andStatusIn(statusList);
        } else {
            // 如果状态的查询条件是空的，则查询没有被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
        }


    }

    /**
     * 修改或者添加
     *
     * @param questionOptionVo
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {
            "1.控制台"}, value = "修改/添加", httpMethod = "POST", notes = "修改时，必须填写“主键”参数。", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionOption.optionId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.questionId", value = "所属的试卷的的主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.content", value = "问题的具体内容", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.analysis", value = "答题解析", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.score", value = "选择后得到的分数", dataType = "float", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.rank", value = "试卷的排序号，数字小的排在前面", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.status", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/save")
    @ResponseBody
    public ResponseEntity save(@ApiIgnore QuestionOptionVo questionOptionVo) throws Exception {
        if (questionOptionVo == null || questionOptionVo.getQuestionOption() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空参数");
        }

        QuestionOption questionOption = questionOptionVo.getQuestionOption();
        String id = questionOption.getOptionId();
        String msg = null;
        if (StringUtils.isNotBlank(id)) {
            // 修改
            int row = questionOptionService.updateByPrimaryKeySelective(questionOption);
            if (row > 0) {
                msg = "修改成功";
            } else {
                msg = "修改失败，可能数据库中不存在这条记录";
            }
        } else {
            // 添加
            int row = questionOptionService.insert(questionOption);
            if (row > 0) {
                msg = "添加成功";
            } else {
                msg = "添加失败，重试";
            }
        }
        return ajaxReturnMap(CODE_SUCCESS, msg, questionOption.getOptionId());
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "根据主键获取记录详情", httpMethod = "GET", notes = "questionId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionOption.optionId", value = "主键id", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseEntity detail(@ApiIgnore QuestionOptionVo questionOptionVo) throws Exception {
        if (questionOptionVo == null
                || questionOptionVo.getQuestionOption() == null
                || StringUtils.isBlank(questionOptionVo.getQuestionOption().getOptionId())) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "optionId是必填");
        }
        String id = questionOptionVo.getQuestionOption().getOptionId();
        QuestionOption questionOption = questionOptionService.selectByPrimaryKey(id);
        return ajaxReturnMap(CODE_SUCCESS, null, questionOption);
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "删除记录", httpMethod = "GET", notes = "optionId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionOption.optionId", value = "主键id", dataType = "String", required = true, paramType = "query")})
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity delete(@ApiIgnore QuestionOptionVo questionOptionVo) throws Exception {
        if (questionOptionVo == null
                || questionOptionVo.getQuestionOption() == null
                || StringUtils.isBlank(questionOptionVo.getQuestionOption().getOptionId())) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "optionId是必填");
        }

        String id = questionOptionVo.getQuestionOption().getOptionId();
        int row = questionOptionService.deleteByPrimaryKey(id);
        String msg = null;
        if (row > 0) {
            msg = "删除成功";
        } else {
            msg = "这条数据在之前已经被删除了";
        }
        return ajaxReturnMap(CODE_SUCCESS, msg, null);
    }

    /**
     * 分页查询
     *
     * @param questionOptionVo
     * @param page
     * @return
     * @throws Exception
     */

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "分页查询", httpMethod = "GET", notes = "可根据条件进行筛选", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "questionOption.optionId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.questionId", value = "所属的试卷的的主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.content", value = "问题的具体内容", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.analysis", value = "答题解析", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.score", value = "选择后得到的分数", dataType = "float", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionOption.rank", value = "试卷的排序号，数字小的排在前面", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "inStatus", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query"),
//            @ApiImplicitParam(name = "associated", value = "是否关联查询,true--关联查询,默认是false", dataType = "boolean", required = false, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码，如果没有值，则使用默认值。开始值从 1 开始计算", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页的数量,如果没有值，则使用默认值", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/pagelist")
    @ResponseBody
    public ResponseEntity pageList(@ApiIgnore QuestionOptionVo questionOptionVo, @ApiIgnore Page page)
            throws Exception {
        QuestionOptionExample example = new QuestionOptionExample();
        this.queryConditions(questionOptionVo, example);
        super.selectByPage(page, example, questionOptionService);
        return ajaxReturnMap(CODE_SUCCESS, null, page);
    }
}
